﻿#region
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using EndlessDownloader.Engine;

#endregion

namespace EndlessDownloaderEx
{
    public partial class ButtonGrid : UserControl
    {
        private List<ButtonEx> _buttons;

        public ButtonGrid()
        {
            InitializeComponent();
        }

        public int SuperNumber { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode)
                return;

            _buttons = new List<ButtonEx>(300);

            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    var button = new ButtonEx();
                    button.Font = new Font("Gulim", 11F, FontStyle.Regular, GraphicsUnit.Point);
                    button.Size = new Size(40, 40);
                    button.TextAlign = ContentAlignment.MiddleCenter;
                    button.FlatStyle = FlatStyle.Popup;
                    button.Click += Button_Click;
                    button.Location = new Point(j * 40, i * 40);
                    button.SuperNumber = SuperNumber;
                    button.SetNo(i * 15 + j + 1);
                    
                    _buttons.Add(button);
                }
            }

            Controls.AddRange(_buttons.ToArray());
        }

        public void SetAsCompleted(int number)
        {
            _buttons.Find(x => x.Number == number).SetAsCompleted();
        }

        public void SetPicture(Picture picture)
        {
            ButtonEx button = _buttons.Find(x => x.Number == picture.Number);
            button.SavedPath = picture.SavePath;
        }

        private void Button_Click(object sender, EventArgs e)
        {
            ButtonEx button = (ButtonEx) sender;
            OnButtonClicked(button.SavedPath);
        }

        #region ButtonClicked event things for C# 3.0
        public event EventHandler<ButtonClickedEventArgs> ButtonClicked;

        protected virtual void OnButtonClicked(ButtonClickedEventArgs e)
        {
            if (ButtonClicked != null)
                ButtonClicked(this, e);
        }

        protected virtual void OnButtonClicked(string savedPath)
        {
            OnButtonClicked(new ButtonClickedEventArgs(savedPath));
        }

        protected virtual ButtonClickedEventArgs OnButtonClickedWithReturn(string savedPath)
        {
            ButtonClickedEventArgs args = new ButtonClickedEventArgs(savedPath);
            OnButtonClicked(args);

            return args;
        }

        public class ButtonClickedEventArgs : EventArgs
        {
            public string SavedPath { get; set; }

            public ButtonClickedEventArgs()
            {
            }

            public ButtonClickedEventArgs(string savedPath)
            {
                SavedPath = savedPath;
            }
        }
        #endregion
    }
}